package com.example.android.ui.hb.detail;

import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.android.R;
import com.example.android.bean.Login;
import com.example.android.bean.SearchHB;
import com.example.android.ui.base.BaseActivity;
import com.example.android.utils.CircleImageView;

public class HBDetailActivity extends BaseActivity implements View.OnClickListener, HBDetailContract.IHBDetailView {
    private ImageView back,image;
    private CircleImageView headImage;
    private Button button;
    private HBDetailPresenter hbDetailPresenter;
    private TextView name,reward,content,username,deadline;
    @Override
    protected void initViews() {
        int hbId = getIntent().getIntExtra("hbId",0);
        back = find(R.id.hb_detail_back);
        button = find(R.id.hb_detail_button);
        image = find(R.id.hb_detail_image);
        headImage = find(R.id.hb_detail_head_image);
        name = find(R.id.hb_detail_name);
        reward = find(R.id.hb_detail_reward);
        content = find(R.id.hb_detail_content);
        username = find(R.id.hb_detail_username);
        deadline = find(R.id.hb_detail_deadline);
        hbDetailPresenter = new HBDetailPresenter(this);
        hbDetailPresenter.getHBDetail(hbId);
        //监听事件
        back.setOnClickListener(this);
        button.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_hb_detail;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.hb_detail_back:
                finish();
                break;
            case R.id.hb_detail_button:
                Toast.makeText(this,"接单成功",Toast.LENGTH_SHORT).show();
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void getHBDetailSuccess(SearchHB searchHB) {
        name.setText(searchHB.getHbName());
        reward.setText(searchHB.getHbReward()+"");
        content.setText(searchHB.getHbIntroduction());
        deadline.setText(searchHB.getHbReleaseDeadlineDatetime());
        Glide.with(this).load(searchHB.getHbImagePath()).into(image);
        SharedPreferences sharedPreferences = getSharedPreferences("HBUserLogin",0);
        String token = sharedPreferences.getString("jwt","");
        hbDetailPresenter.getRelease(searchHB.getHbReleaseUserid(),token);
    }

    @Override
    public void getReleaseSuccess(Login.UserInfoDTO userInfoDTO) {
        username.setText(userInfoDTO.getUserUsername());
        Log.i("HBDetailActivity1",userInfoDTO.getUserHeadimageUrl());
        Glide.with(this).load(userInfoDTO.getUserHeadimageUrl()).into(headImage);
    }

    @Override
    public void getHBDetailError(Throwable throwable) {
        Log.e("HBDetailActivity",throwable.toString());
    }

    @Override
    public void getReleaseError(Throwable throwable) {
        Log.e("HBDetailActivity",throwable.toString());
    }
}
